// public void lockAThenB() {
//   synchronized (lockA) {
//     synchronized (lockB) {
//       // do something with both resources
//     }
//   }
// }

// public void lockBThenA() {
//   synchronized (lockB) {
//     synchronized (lockA) {
//       // do something with both resources
//     }
//   }
// }

public class deadlock {
  private static final Object lockA = new Object();
  private static final Object lockB = new Object();

  public static void lock() {
    // 线程1：先锁A，再锁B
        Thread t1 = new Thread(() -> {
            synchronized (lockA) {
                System.out.println("Thread 1: locked A");
                try {
      Thread.sleep(100); } catch (InterruptedException e) {}
                synchronized (lockB) {
      System.out.println("Thread 1: locked B");
                }
  }
});

// 线程2：先锁B，再锁A
        Thread t2 = new Thread(() -> {
            synchronized (lockB) {
                System.out.println("Thread 2: locked B");
                try {
  Thread.sleep(100); } catch (InterruptedException e) {}
                synchronized (lockA) {
  System.out.println("Thread 2: locked A");
                }
        }
        });

        t1.start();
        t2.start();
        }
        }